package com.ruoyi.activiti.service.impl;

import com.ruoyi.activiti.entity.JobProcessQueryRequest;
import com.ruoyi.activiti.entity.JobProcessQueryResponse;
import com.ruoyi.activiti.service.IJobApprovalActiviti;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.repository.ProcessDefinitionQuery;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class JobApprovalActivitiImpl implements IJobApprovalActiviti {

    private void init() {
    }

    @Override
    public JobProcessQueryResponse queryProcess(JobProcessQueryRequest jobProcessQueryRequest) {
        JobProcessQueryResponse jobProcessQueryResponse = new JobProcessQueryResponse();
        //创建ProcessEngine
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //得到RepositoryService实例
        RepositoryService repositoryService = processEngine.getRepositoryService();
        //使用RepositoryService进行部署
        Deployment deployment = repositoryService.createDeployment()
                //queryBpmnByJobType(jobProcessQueryRequest)
                .addClasspathResource("bpmn/leave.bpmn20.xml") // 添加bpmn资源
                //.addClasspathResource("bpmn/evection.png")  // 添加png资源
                //queryBpmnNameByJobType(jobProcessQueryRequest)
                .name("动火作业票流程")
                .deploy();

        jobProcessQueryResponse.setDeploymentId(deployment.getId());
        jobProcessQueryResponse.setDeploymentName(deployment.getName());

        //得到ProcessDefinitionQuery 对象
        ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery();

        List<ProcessDefinition> definitionList = processDefinitionQuery.processDefinitionName("动火作业票流程")
                .orderByProcessDefinitionVersion()
                .desc()
                .list();

        for (ProcessDefinition processDefinition : definitionList) {
            System.out.println("流程定义 id=" + processDefinition.getId());
            System.out.println("流程定义 name=" + processDefinition.getName());
            System.out.println("流程定义 key=" + processDefinition.getKey());
            System.out.println("流程定义 Version=" + processDefinition.getVersion());
            System.out.println("流程部署ID =" + processDefinition.getDeploymentId());
        }

        return jobProcessQueryResponse;
    }

    private String queryBpmnByJobType(JobProcessQueryRequest jobProcessQueryRequest) {
        switch (jobProcessQueryRequest.getJobType()) {
            case "DH":
                return "dh.bpmn";
            default:
                return "";
        }
    }

    private String queryBpmnNameByJobType(JobProcessQueryRequest jobProcessQueryRequest) {
        switch (jobProcessQueryRequest.getJobType()) {
            case "DH":
                return "动火作业票审批流程";
            default:
                return "";
        }
    }

}
